package com.rxt.thermopro.adapter

import android.annotation.SuppressLint
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.bean.ProfileItem
import kotlinx.android.synthetic.main.item_view_bluetooth.view.tv_name
import kotlinx.android.synthetic.main.item_view_profile_item.view.*
import kotlinx.android.synthetic.main.item_view_set_profile.view.*

class ProfileItemAdapter(var list: MutableList<ProfileItem>) :
    RecyclerView.Adapter<ProfileItemAdapter.MyViewHolder>() {

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    var onClick: OnItemClickListener? = null

    fun setOnItemClick(onItemClickListener: OnItemClickListener) {
        this.onClick = onItemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.item_view_profile_item, parent, false)
        itemView.setOnClickListener {

        }
        return MyViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.itemView.tv_name.text = list[position].itemName
        if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F &&
            list[position].itemValueF.isNotEmpty()
        ) {
            holder.itemView.tv_value.text =
                list[position].itemValueF + LocalDataDao.INSTANCE.getTempUnit()
        } else {
            holder.itemView.tv_value.text =
                LocalDataDao.INSTANCE.convertUnit(list[position].itemValue)
        }
        holder.itemView.setOnClickListener {
            onClick?.onItemClick(holder.itemView, position)
        }
        holder.itemView.isSelected = list[position].checked
    }

    fun add(bean: ProfileItem) {
        Log.d("112121->adapter", bean.itemName + "\n" + bean.itemValue)
        list.add(bean)
        notifyDataSetChanged()
    }

    fun hasItem(value: String?): Boolean {
        var flag = -1
        list.forEach {
            if (it.itemValue == value) {
                flag = 1
            }
        }
        return flag == 1
    }

    fun addAll(itList: List<ProfileItem>) {
        list.addAll(itList)
        notifyDataSetChanged()
    }

    fun get(position: Int): ProfileItem {
        return list[position]
    }

    fun clear() {
        list.clear()
        notifyDataSetChanged()
    }

    fun checkPosition(position: Int) {
        var index = -1
        var index1 = -1
        for (i in 0 until list.size) {
            if (i == position && !list[i].checked) {
                index = i
                list[i].checked = true
            } else if (list[i].checked) {
                index1 = i
                list[i].checked = false
            }
        }
        if (index != -1) {
            notifyItemChanged(index)
        }
        if (index1 != -1) {
            notifyItemChanged(index1)
        }
    }

    fun update(itemBean: ProfileItem, position: Int) {
        var index1 = -1
        var index = -1
        for (i in 0 until list.size) {
            if (i != position && list[i].checked && list[i].itemName != itemBean.itemName) {
                index1 = i
                list[i].checked = false
            } else if (i == position && list[i].itemName == itemBean.itemName) {
                index = i
                if (!list[i].checked) {
                    list[i].checked = true
                }
            }
        }
        if (index1 != -1) {
            notifyItemChanged(index1)
        }
        if (index != -1) {
            notifyItemChanged(index)
        }
    }

    fun clearChecked() {
        list.forEach {
            it.checked = false
        }
        notifyDataSetChanged()
    }

    interface OnItemClickListener {
        fun onItemClick(view: View, position: Int)
    }

}